from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
   def handle_starttag(self, tag, attrs):
#      print "handle_starttag"
#      print "tag:", tag
#      print "attrs:", attrs
#      print self.stack
#      print len(self.stack)
#      raw_input("  ")
      if len(tag) == 1 and "p" in tag:
         for i in attrs:
            if "data-pid" in i:
               # turn on capture
               self.stack[0] = 1 
               self.stack.append("---BEGIN---")
      if self.stack[0] == 1:
         if len(tag) == 1 and "a" in tag:
            self.stack.append(attrs)
            print "hello"
            print type(attrs)

   def handle_endtag(self, tag):
#      print "handle_end"
#      print "tag:", tag
#      print self.stack
#      print len(self.stack)
#      raw_input(" ")
      if self.stack[0] == 1:
         if len(tag) == 1 and "p" in tag:
            # turn off capture
            self.stack[0] = 0 
            self.stack.append("---END---")
      
   def handle_data(self, data):
#      print "handle_data"
#      print "data:", data
#      print self.stack
#      print len(self.stack)
#      raw_input(" ")
      st = data.lstrip().rstrip()
      if len(st) >0:
         if len(self.stack) > 0:
            if self.stack[0] == 1:
               self.stack.append(st)
      
   def get_row(self, line):
      self.stack = []
      self.stack.append(0)   # start in off mode
      self.feed(line)
      if len(self.stack) > 1:
         for i in self.stack:
            print i

    
  
def test():
   p = MyHTMLParser()
#   f = open("msg.html", "r")
   f = open("test.html", "r")
#   line = f.readline()
   line = f.read()
   p.get_row(line)

#   while (len(line) !=0):
#      p.get_row(line)
#      print "\n--new readline--\n"
#      line = f.readline()
   f.close()

if __name__ == "__main__":
   test()
